package com.toobigtofail
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Sfx;
	import net.flashpunk.World;
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	public class Menu extends World
	{
		[Embed(source="/assets/gfx/start-game-btn.png")] private static const START_BTN_PNG:Class;
		[Embed(source="/assets/sfx/introsong.mp3")] private const MENU_MUSIC:Class;

		
		private var _display:Entity = new Entity(FP.screen.width/2 - 100,FP.screen.height/2 - 40);
		private var _inputDisplay:InputDisplay = new InputDisplay(FP.screen.width/2 - 60, FP.screen.height/2);
		private var _start_btn_image:Image = new Image(START_BTN_PNG);
		private var _start_btn:Entity = new Entity(FP.screen.width/2 - _start_btn_image.width/2, FP.screen.height/2 + 100, _start_btn_image);
		private var _music:Sfx = new Sfx(MENU_MUSIC);
		
		public function Menu()
		{
			super();
		}
		
		override public function begin():void {
			var gl:Graphiclist = new Graphiclist();
			gl.add(new Text("Snakation in Beautiful Ouroboros", -50, -200));
			gl.add(new Text("Enter your group name:"));
			gl.add(new Text("Choose the same name as up to 3 friends to play with them!",-150,16));
			//gl.add(new Text("A Game by Troy Fendall, Paul Maupoux, Connor Peavey,",-75,16));
			//gl.add(new Text("Justin Peavey, Reilly Peavey, and Chris Rodgers.",-90,16));
			_display.graphic = gl;
			add(_display);
			add(_inputDisplay);
			_start_btn.setHitbox(_start_btn_image.width, _start_btn_image.height);
			_start_btn.visible = false;
			add(_start_btn);
			_music.loop();
		}
		
		override public function update():void {
			super.update();
			_start_btn.visible = _inputDisplay.length() > 0;
			
			// Check for word game entry
			var k:int = -1;
			if (Input.pressed(Key.ANY) || Input.check(Key.BACKSPACE)) {
				k = _inputDisplay.keypress(Input.lastKey);
			}
			
			if (_start_btn.visible && (Key.ENTER == k || Input.mouseReleased && _start_btn.collidePoint(_start_btn.x, _start_btn.y, Input.mouseX, Input.mouseY))) {
				_music.stop();
				FP.world = new Game(_inputDisplay.text);
			}
		}
	}
}